﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;

namespace HotelManagement._01.FORM
{
    public partial class F11_UserManagement : Form
    {
        User m_User;
        UserBUS m_UserBUS;
        public F11_UserManagement()
        {
            InitializeComponent();
            Init();
            LoadDataToGrid();
        }

        private void Init()
        {
            m_User = new User();
            m_UserBUS = new UserBUS();
            RoleBUS m_RoleBUS = new RoleBUS();
            m_cbb_RoleID.DataSource = m_RoleBUS.ViewRole();
            m_cbb_RoleID.DisplayMember = "RoleDescription";
            m_cbb_RoleID.ValueMember = "RoleID";
            m_cbb_Gender.SelectedItem = "Nam";
        }
        private void LoadDataToGrid()
        {
            m_dgv_User.DataSource = m_UserBUS.ViewUser();
        }
        private User GetDataFromForm()
        {
            User v_User = new User();
            v_User.UserName = m_txt_UserName.Text;
            v_User.FullName = m_txt_FullName.Text;
            v_User.Note = m_txt_Note.Text;
            v_User.Password = m_txt_Password.Text;
            v_User.Email = m_txt_Email.Text;
            v_User.Gender = m_cbb_Gender.SelectedItem.ToString();
            if (CValidate.IsNumber(m_cbb_RoleID.SelectedValue.ToString())) v_User.RoleID = int.Parse(m_cbb_RoleID.SelectedValue.ToString());
            if (CValidate.IsNumber(m_txt_UserID.Text)) v_User.UserID = int.Parse(m_txt_UserID.Text);
            return v_User;
        }
        private void SetDataToForm(User ip_User)
        {
            m_txt_UserID.Text = ip_User.UserID.ToString();
            m_cbb_Gender.SelectedItem = ip_User.Gender;
            m_cbb_RoleID.SelectedValue = ip_User.RoleID.ToString();
            m_txt_Note.Text = ip_User.Note;
            m_txt_Email.Text =ip_User.Email;
            m_txt_FullName.Text = ip_User.FullName;
            m_txt_UserName.Text = ip_User.UserName;
            m_txt_UserID.Text = ip_User.UserID.ToString();
            m_txt_Password.Text = ip_User.Password;
            m_txt_ConfirmPassword.Text = ip_User.Password;
        }
        private void ResetText()
        {
            m_cbb_Gender.Text = "Nam";
            m_cbb_RoleID.Text = "";
            m_txt_Note.Text="";
            m_txt_Email.Text = "";
            m_txt_FullName.Text = "";
            m_txt_Keyword.Text = "";
            m_txt_UserName.Text = "";
            m_txt_UserID.Text = "";
            m_txt_ConfirmPassword.Text = "";
            m_txt_Password.Text = "";
            m_txt_Password.Enabled = true;
            m_txt_ConfirmPassword.Enabled = true;

        }
        private bool ValidateForm()
        {
            if (CValidate.IsNullString(m_txt_UserName.Text)) { MessageBox.Show("Bạn chưa nhập tên người dùng !"); return false; }
            if (CValidate.IsNullString(m_txt_FullName.Text)) { MessageBox.Show("Bạn chưa nhập họ và tên người dùng !"); return false; }
            if (CValidate.IsNullString(m_txt_Password.Text)) { MessageBox.Show("Bạn chưa nhập mật khẩu !"); return false; }
            if (CValidate.IsNullString(m_txt_Email.Text)) { MessageBox.Show("Bạn chưa nhập email !"); return false; }
            if (!CValidate.IsEmail(m_txt_Email.Text)) { MessageBox.Show("Email không đúng !"); return false; }
            if (CValidate.IsNullString(m_txt_ConfirmPassword.Text)) { MessageBox.Show("Bạn chưa nhập xác nhận mật khẩu !"); return false; }
            if (m_txt_ConfirmPassword.Text != m_txt_Password.Text) { MessageBox.Show("Mật khẩu xác nhận chưa đúng !"); return false; }
            return true;

        }
        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (m_txt_UserID.Text.Trim() != "") return;
            if (!ValidateForm()) return;
            try 
	        {   
                m_User = GetDataFromForm();
		        if (m_UserBUS.AddUser(m_User)) MessageBox.Show("Thêm nhân viên thành công");
	        }
	        catch (Exception v_e)
	        {
		        throw v_e;
	        }
            ResetText();
            LoadDataToGrid();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            if (m_txt_UserID.Text.Trim() == "") return;
            if (!ValidateForm()) return;
            try
            {
                m_User = GetDataFromForm();
                if (m_UserBUS.UpdateUser(m_User)) MessageBox.Show("Sửa nhân viên thành công");
            }
            catch (Exception v_e)
            {
                throw v_e;
            }
            ResetText();
            LoadDataToGrid();
        }

        private void m_dgv_User_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_User.UserID =int.Parse(m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["UserID"].Value.ToString());
                m_User.UserName = m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["UserName"].Value.ToString();
                m_User.FullName = m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["FullName"].Value.ToString();
                m_User.Password = m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["Password"].Value.ToString();
                m_User.Email = m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["Email"].Value.ToString();
                m_User.Gender = m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["Gender"].Value.ToString();
                m_User.RoleID = int.Parse(m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["RoleID"].Value.ToString());
                m_User.Note = m_dgv_User.Rows[m_dgv_User.CurrentCell.RowIndex].Cells["Note"].Value.ToString();
                m_txt_Password.Enabled = false;
                m_txt_ConfirmPassword.Enabled = false;
                SetDataToForm(m_User);
            }
            catch (Exception)
            {

                MessageBox.Show("Bạn chưa chọn nhân viên nào !");
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (m_txt_UserID.Text.Trim() == "") return;
            if (MessageBox.Show("Bạn có chắc là bạn muốn xóa thông tin này ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (m_UserBUS.DeleteUser(m_User)) MessageBox.Show("Đã xóa thành công !"); else MessageBox.Show("Lỗi không thể xóa !");
                LoadDataToGrid();
            }
            ResetText();
        }

        private void Reset(object sender, EventArgs e)
        {
            ResetText();
        }

        private void search_Click(object sender, EventArgs e)
        {
            m_dgv_User.DataSource = m_UserBUS.SearchUser(m_txt_Keyword.Text);
            m_txt_Keyword.Text = "";
        }

        private void m_dgv_User_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }      
    }
}
